package com.bw.sofa_module.contentsofa;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bw.adapter_sdk.quick.BaseQuickAdapter;
import com.bw.common.entity.LiveDataEntity;
import com.bw.common.mvvm.view.BaseFragment;
import com.bw.network_sdk.videoentity.VideoEntity;
import com.bw.network_sdk.videoentity.VideoValuesEntity;
import com.bw.sofa_module.R;
import com.bw.sofa_module.databinding.FragmentContentBinding;
import com.bw.sofa_module.entity.diss.DissEntity;
import com.bw.sofa_module.entity.likestate.LikeStateEntity;
import com.bw.sofa_module.picturesofa.PictureAdapter;
import com.bw.sofa_module.videosofa.VideoAdapter;

import java.io.Serializable;
import java.util.List;

public class ContentFragment extends BaseFragment<FragmentContentBinding,ContentViewModel>
        implements BaseQuickAdapter.OnItemClickChildListener, BaseQuickAdapter.OnItemClickListener {
    private ContentAdapter contentAdapter;
    private int position = -1;
    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        return FragmentContentBinding.inflate(inflater);
    }

    @Override
    public void initView() {
        v.contentsofaRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        contentAdapter = new ContentAdapter();
        contentAdapter.setOnItemClickChildListener(this);
        contentAdapter.setOnItemClickListener(this);
        v.contentsofaRecycler.setAdapter(contentAdapter);
    }

    @Override
    public void initData() {

    }

    @Override
    public void handleMessage(LiveDataEntity entity) {
        switch (entity.action) {
            case START_VIEW_MODEL:
                VideoEntity videoEntity = (VideoEntity) entity.data.get(LiveDataEntity.LiveDataType.ENTITY);
                contentAdapter.setNewInstance(videoEntity.data.data);
                break;
            case LIKES:
                LikeStateEntity likeEntity = (LikeStateEntity) entity.data.get(LiveDataEntity.LiveDataType.ENTITY);
                contentAdapter.getData().get(position).ugc.hasLiked = likeEntity.data.data.hasLiked;
                if (likeEntity.data.data.hasLiked)
                    contentAdapter.getData().get(position).ugc.likeCount = vm.count;
                contentAdapter.notifyItemChanged(position);
                break;
            case DISS:
                DissEntity dissEntity = (DissEntity) entity.data.get(LiveDataEntity.LiveDataType.ENTITY);
                contentAdapter.getData().get(position).ugc.hasdiss = dissEntity.data.data.hasLiked;
                contentAdapter.notifyItemChanged(position);
                break;
        }
    }

    @Override
    public void onItemClickChild(RecyclerView.Adapter adapter, int id, int position) {

        ContentAdapter contentAdapters = (ContentAdapter) adapter;
        List<VideoValuesEntity> data = contentAdapters.getData();
        if (id == R.id.item_content_likes_img) {
            Toast.makeText(getContext(), "卡萨丁解放碑", Toast.LENGTH_SHORT).show();
            this.position = position;
            vm.onLike(data.get(position).id,(int)data.get(position).itemId);
        } else if (id == R.id.item_content_unlikes_img) {
            this.position = position;
            vm.onDiss(data.get(position).itemId);
        } else if (id == R.id.item_content_comment_img) {
        } else if (id == R.id.item_content_share_img) {
        }
    }

    @Override
    public void onItemClick(RecyclerView.Adapter adapter, int position) {
        if (adapter instanceof ContentAdapter){
            ContentAdapter contentAdapter = (ContentAdapter) adapter;
            VideoValuesEntity videoValuesEntity = contentAdapter.getData().get(position);
            Bundle bundle = new Bundle();
            videoValuesEntity.itemType = 0;
            bundle.putSerializable("videoEntity", (Serializable) videoValuesEntity);
            Log.i("ggg",videoValuesEntity.toString());
            ARouter.getInstance().build("/sofa/videodetail")
                    .withBundle("bundle",bundle)
                    .navigation(getContext());
        }
    }
}